<?php
class Contact extends AdminController {
    public function __construct() {
        parent::__construct();
        session_start();
        $_SESSION["KCFINDER"]["disabled"]=false;
        $_SESSION["KCFINDER"]["uploadURL"]=base_url()."uploads/contacts/";
    }
    
    public function index() {
        $this->_data["loadPage"]="contact/index_view";
        $this->_data["titlePage"]="Quản lý liên hệ";
        $this->_data["mess_succ"]=$this->session->flashdata("flash_mess");
        
        $this->session->unset_userdata("sess_search_fullname");
        $this->session->unset_userdata("sess_search_email");
        $this->session->unset_userdata("sess_search_datefrom");
        $this->session->unset_userdata("sess_search_dateto");
        
        $this->load->model("mcontact");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/contact/index/",
            "total_rows"=>$this->mcontact->countAll(),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mcontact->listAll($config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mcontact->listAll($config["per_page"], $start);
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function search() {
        $this->_data["loadPage"]="contact/search_view";
        $this->_data["titlePage"]="Tìm kiếm liên hệ";
        
        $fullname=$this->session->userdata("sess_search_fullname");
        $email=$this->session->userdata("sess_search_email");
        $datefrom=$this->session->userdata("sess_search_datefrom");
        $dateto=$this->session->userdata("sess_search_dateto");
        
        $this->load->model("mcontact");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/contact/search/",
            "total_rows"=>$this->mcontact->countAllResults($fullname, $email, $datefrom, $dateto)["count"],
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mcontact->search($fullname, $email, $datefrom, $dateto, $config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mcontact->search($fullname, $email, $datefrom, $dateto, $config["per_page"], $start);
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function searchdata() {
        if(isset($_POST["btnSearchContact"])) {
            $sess=array(
                "sess_search_fullname"=>$this->input->post("txtFullname"),
                "sess_search_email"=>$this->input->post("txtEmail"),
                "sess_search_datefrom"=>$this->input->post("txtDateFrom"),
                "sess_search_dateto"=>$this->input->post("txtDateTo"),
            );
            $this->session->set_userdata($sess);
            redirect(base_url()."admin_combo/contact/search");
        }
    }
    
    public function add() {
        $this->_data["loadPage"]="contact/add_view";
        $this->_data["titlePage"]="Thêm liên hệ";
        
        //validate
        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->CI =& $this;
        
        $config=array(
            array(
                "field"=>"txtFullName",
                "label"=>"Họ Tên",
                "rules"=>"required"
            )
        );
        $this->form_validation->set_rules($config);
        
        //add
        if($this->form_validation->run()) {
            $data=array(
                "con_detail"=>$this->input->post("txtDetail"),
                "con_date"=>date("Y-m-d H:i:s"),
                "fullname"=>$this->input->post("txtFullName"),
                "email"=>$this->input->post("txtEmail"),
                "content"=>$this->input->post("txtContent")
            );
            $this->load->model("mcontact");
            $this->mcontact->insert($data);
            $this->session->set_flashdata("flash_mess", "Thêm liên hệ thành công.");
            redirect(base_url()."admin_combo/contact/");
        }
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function edit() {
        $this->_data["loadPage"]="contact/edit_view";
        $this->_data["titlePage"]="Chỉnh sửa liên hệ";
        
        $id=$this->uri->segment(4);
        $this->load->model("mcontact");
        $this->_data["info_edit"]=$this->mcontact->getById($id);
        //validate
        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->CI =& $this;
        
        $config=array(
            array(
                "field"=>"txtFullName",
                "label"=>"Họ Tên",
                "rules"=>"required"
            )
        );
        $this->form_validation->set_rules($config);
        
        //add
        if($this->form_validation->run()) {
            $data=array(
                "con_detail"=>$this->input->post("txtDetail"),
                "fullname"=>$this->input->post("txtFullName"),
                "email"=>$this->input->post("txtEmail"),
                "content"=>$this->input->post("txtContent")
            );
            $this->mcontact->update($data, $id);
            $this->session->set_flashdata("flash_mess", "Chỉnh sửa liên hệ thành công.");
            redirect(base_url()."admin_combo/contact/");
        }
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function del() {
        $id=$this->uri->segment(4);
        $this->load->model("mcontact");
        $this->mcontact->del($id);
        $this->session->set_flashdata("flash_mess", "Xóa liên hệ thành công.");
            redirect(base_url()."admin_combo/contact/");
    }
}
?>
